-- 本地化支持

-- 根据语言版本加载不同的文件
local loc_table = nil;
local csv_loc_table = nil;
local skill_loc_table = nil;
local monster_loc_table = nil;

function setLang(lang)
    UserDefaultM.setStringForKey("APP_LANG", lang);
end

function getLang()
    local lang = UserDefaultM.getStringForKey("APP_LANG");
    if lang == nil or lang == "" then
        -- 默认语言版本
        lang = "en";
    end

    if PROOFREADER_MODE ~= 1 and table.indexOf(getProofeaderLang(), lang) ~= -1 then
        -- 非语言校对模式，但属于待校对语言，则默认使用英语
        lang = "en";
    end

    return lang;
end

-- 获取发布版本语言
function getPublishLang()
    return { "zh", "tw", "en", "ja", "th", "fr", "es", "pt", "ru", "de", "ko", };
end

-- 获取校对版本语言
function getProofeaderLang()
    return {  };
end

-- 获取所有语言版本
function getAllLang()
    local publishLang, proofeaderLang = getPublishLang(), getProofeaderLang();
    return table.append(publishLang, proofeaderLang);
end

function getLocKerning()
    -- 获取文本额外的缩进值
    if getLang() == "en" then
        return 0;
    end

    if getLang() == "ja" then
        return -1;
    end

    return 0;
end

function getBtnFontSizeFix()
    local fontSizeFix = 2;

    if getLang() == "ja" then
        fontSizeFix = 2;
    end

    return fontSizeFix;
end

function getBtnKerningFix()
    local kerningFix = -3;

    if getLang() == "ja" then
        kerningFix = -3;
    end

    return kerningFix;
end

function kerningFix(kerning)
    -- 泰语的kerning如果小于0， 显得太拥挤，修正为0
    if getLang() == "th" and kerning < 0 then
        kerning = -1;
    end

    return kerning;
end

function getLocFontName()
    -- 根据语言版本选择字体文件
    local lang = getLang();

    if lang == "zh" then
        return "fonts/simhei.ttf";
    elseif lang == "tw" then
        return "fonts/simhei.ttf";
    elseif lang == "ja" then
        return "fonts/simhei.ttf";
    elseif lang == "ko" then
        return "fonts/simhei.ttf";
    elseif lang == "th" then
        return "fonts/cordia.ttf";
    else
        if getLang() == "en" then
            return "fonts/impact.ttf";
        else
            return "fonts/simhei.ttf";
        end
    end
end

function getLocFontSizeFactor(str)
    -- 根据语言版本选择字体的横向缩放比例
    -- 需要用的地方，用返回值与字体大小相乘
    if getLang() == "en" then
        -- 英文版本要根据字符来区分
        local arr1 = { "m", "M", "w", "W",  };
        local arr2 = { "r", "t", "i", "I", "l", };

        if table.indexOf(arr1, str) ~= -1 then
            return 0.72;
        elseif table.indexOf(arr2, str) ~= -1 then
            return 0.36;
        else
            return 0.5;
        end
    else
        return 1;
    end
end

function getLocFontSizeMulti(noFix)
    if getLang() == "en" and not noFix then
        return 0.8;
    end

    if getLang() == "ja" and not noFix then
        return 0.87;
    end

    if getLang() == "th" and not noFix then
        return 1.15;
    end

    if getLang() == "tw" and not noFix then
        return 0.95;
    end

    if getLang() == "zh" and not noFix then
        return 0.95;
    end

    if getLang() == "ko" and not noFix then
        return 0.87;
    end

    if getLang() == "pt" and not noFix then
        return 0.7;
    end

    if getLang() == "fr" and not noFix then
        return 0.75;
    end

    if getLang() == "es" and not noFix then
        return 0.7;
    end

    if getLang() == "ru" and not noFix then
        return 0.7;
    end

    if getLang() == "de" and not noFix then
        return 0.7;
    end

    return 1;
end

function initLang()
    local lang = getLang();
    if _G["gameScene"] then
        local locFile = "game/loc/loc_en";

        if lang == "zh" then
            locFile = "game/loc/loc_zh";
        elseif lang == "tw" then
            locFile = "game/loc/loc_tw";
        elseif lang == "en" then
            locFile = "game/loc/loc_en";
        elseif lang == "th" then
            locFile = "game/loc/loc_th";
        elseif lang == "ja" then
            locFile = "game/loc/loc_ja";
        elseif lang == "pt" then
           locFile = "game/loc/loc_pt";
        elseif lang == "fr" then
           locFile = "game/loc/loc_fr";
        elseif lang == "es" then
           locFile = "game/loc/loc_es";
        elseif lang == "ru" then
            locFile = "game/loc/loc_ru";
        elseif lang == "de" then
            locFile = "game/loc/loc_de";
        elseif lang == "ko" then
            locFile = "game/loc/loc_ko";
        end

        if not isWindowsPlatform() and not cc.FileUtils:getInstance():isFileExist(locFile..".luac") then
            locFile = "game/loc/loc_en";
        end

        loc_table = require(locFile);
        table.insert(_G["myRequire"], locFile);
        return;
    end

    if lang == "zh" then
        loc_table  = require "game/loc/loc_zh";
        csv_loc_table = require "game/loc/csv_loc_zh";
        skill_loc_table = require "game/loc/skill_loc_zh";
        monster_loc_table = require "game/loc/monster_loc_zh";
        dialog_loc_table  = require "game/loc/dialog_loc_zh";
    elseif lang == "tw" then
        loc_table  = require "game/loc/loc_tw";
        csv_loc_table = require "game/loc/csv_loc_tw";
        skill_loc_table = require "game/loc/skill_loc_tw";
        monster_loc_table = require "game/loc/monster_loc_tw";
        dialog_loc_table  = require "game/loc/dialog_loc_tw";
    elseif lang == "en" then
        loc_table  = require "game/loc/loc_en";
        csv_loc_table = require "game/loc/csv_loc_en";
        skill_loc_table = require "game/loc/skill_loc_en";
        monster_loc_table = require "game/loc/monster_loc_en";
        dialog_loc_table  = require "game/loc/dialog_loc_en";
    elseif lang == "th" then
        loc_table  = require "game/loc/loc_th";
        csv_loc_table = require "game/loc/csv_loc_th";
        skill_loc_table = require "game/loc/skill_loc_th";
        monster_loc_table = require "game/loc/monster_loc_th";
        dialog_loc_table  = require "game/loc/dialog_loc_th";
    elseif lang == "ja" then
        loc_table  = require "game/loc/loc_ja";
        csv_loc_table = require "game/loc/csv_loc_ja";
        skill_loc_table = require "game/loc/skill_loc_ja";
        monster_loc_table = require "game/loc/monster_loc_ja";
        dialog_loc_table  = require "game/loc/dialog_loc_ja";
    elseif lang == "pt" then
        loc_table  = require "game/loc/loc_pt";
        csv_loc_table = require "game/loc/csv_loc_pt";
        skill_loc_table = require "game/loc/skill_loc_pt";
        monster_loc_table = require "game/loc/monster_loc_pt";
        dialog_loc_table  = require "game/loc/dialog_loc_pt";
    elseif lang == "fr" then
        loc_table  = require "game/loc/loc_fr";
        csv_loc_table = require "game/loc/csv_loc_fr";
        skill_loc_table = require "game/loc/skill_loc_fr";
        monster_loc_table = require "game/loc/monster_loc_fr";
        dialog_loc_table  = require "game/loc/dialog_loc_fr";
    elseif lang == "es" then
        loc_table  = require "game/loc/loc_es";
        csv_loc_table = require "game/loc/csv_loc_es";
        skill_loc_table = require "game/loc/skill_loc_es";
        monster_loc_table = require "game/loc/monster_loc_es";
        dialog_loc_table  = require "game/loc/dialog_loc_es";
    elseif lang == "ru" then
        loc_table  = require "game/loc/loc_ru";
        csv_loc_table = require "game/loc/csv_loc_ru";
        skill_loc_table = require "game/loc/skill_loc_ru";
        monster_loc_table = require "game/loc/monster_loc_ru";
        dialog_loc_table  = require "game/loc/dialog_loc_ru";
    elseif lang == "de" then
        loc_table  = require "game/loc/loc_de";
        csv_loc_table = require "game/loc/csv_loc_de";
        skill_loc_table = require "game/loc/skill_loc_de";
        monster_loc_table = require "game/loc/monster_loc_de";
        dialog_loc_table  = require "game/loc/dialog_loc_de";
    elseif lang == "ko" then
        loc_table  = require "game/loc/loc_ko";
        csv_loc_table = require "game/loc/csv_loc_ko";
        skill_loc_table = require "game/loc/skill_loc_ko";
        monster_loc_table = require "game/loc/monster_loc_ko";
        dialog_loc_table  = require "game/loc/dialog_loc_ko";
    end

    default_loc_table  = require "game/loc/loc_en";
    default_csv_loc_table = require "game/loc/csv_loc_en";
    default_skill_loc_table = require "game/loc/skill_loc_en";
    default_monster_loc_table = require "game/loc/monster_loc_en";
    default_dialog_loc_table  = require "game/loc/dialog_loc_en";
end

function getLocStr(key)
    if loc_table[key] == nil then
        if default_loc_table[key] == nil then
            return key;
        else
            return default_loc_table[key];
        end
    end

    return loc_table[key];
end

function getDialogStr(key)
    if dialog_loc_table[key] then
        return dialog_loc_table[key];
    elseif loc_table[key] then
        return loc_table[key];
    elseif default_dialog_loc_table[key] then
        return default_dialog_loc_table[key];
    else
        return key;
    end
end

function getSkillLocStr(key)
    if skill_loc_table[key] == nil then
        if default_skill_loc_table[key] == nil then
            return key;
        else
            return default_skill_loc_table[key];
        end
    end

    return skill_loc_table[key];
end

function getMonsterLocStr(key)
    if monster_loc_table[key] == nil then
        if default_monster_loc_table[key] == nil then
            return key;
        else
            return default_monster_loc_table[key];
        end
    end

    return monster_loc_table[key];
end

function getCsvLocStr(key)
    for k, v in pairs(csv_loc_table) do
        if v[key] ~= nil then
            return v[key];
        end
    end

    for k, v in pairs(default_csv_loc_table) do
        if v[key] ~= nil then
            return v[key];
        end
    end

    return key;
end

function covertNumToRoman(num)
    local numTable = { [1] = "I", [2] = "II", [3] = "III", [4] = "IV",
                       [5] = "V", [6] = "VI", [7] = "VII", [8] = "VIII",
                       [9] = "IX", [10] = "X", }

    if num > 10 and num < 20 then
        local index1 = math.modf(num / 10);
        local index2 = num % 10;
        return numTable[index1] .. numTable[index2];
    elseif num > 0 then
        return numTable[num];
    else
        return "O";
    end
end

function covertNumToCapital(num)
    if num > 9 then
        -- 暂时先处理个位数
        return "";
    end

    if num == 9 then
        return getLocStr("number_nine");
    elseif num == 8 then
        return getLocStr("number_eight");
    elseif num == 7 then
        return getLocStr("number_seven");
    elseif num == 6 then
        return getLocStr("number_six");
    elseif num == 5 then
        return getLocStr("number_five");
    elseif num == 4 then
        return getLocStr("number_four");
    elseif num == 3 then
        return getLocStr("number_three");
    elseif num == 2 then
        return getLocStr("number_two");
    elseif num == 1 then
        return getLocStr("number_one");
    elseif num == 0 then
        return getLocStr("number_zero");
    end
end

-- 是否中文版本
function isChineseLang()
    local lang = getLang();

    if lang == "zh" then
        return true;
    elseif lang == "tw" then
        return true;
    else
        return false;
    end
end

initLang();
